﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using SpLocalizer.Common;
using SpLocalizer.Common.DbApi;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Navigation;

namespace SpLocalizer.ContentSourceProviders.SpDocumentLibraryFile
{

    /// <summary>
    /// Content provider for Document library files
    /// </summary>
    public class SpDocumentLibraryFileContent : AbstractContent, IDisposable
    {

        private const string RES_NAME = "SpLocalizer.ContentSourceProviders.SpDocumentLibraryFile.SPQuery.xml";

        private SPList list = null;

        /// <summary>
        /// Return query from resource file
        /// </summary>
        private string Query
        {
            get
            {
                using (Stream stream = typeof(SpDocumentLibraryFileContent).Assembly.GetManifestResourceStream(RES_NAME))
                {
                    StreamReader reader = new StreamReader(stream);
                    return reader.ReadToEnd();
                }
            }
        }


        public override IEnumerable<BaseTranslationTask> BuildTranslationTasks(CultureInfo FromLanguage, CultureInfo ToLanguage)
        {
            List<BaseTranslationTask> tasksToTranslate = new List<BaseTranslationTask>();
            SPQuery query = new SPQuery();
            query.ViewAttributes = "Scope='RecursiveAll'"; 
            query.Query = Query;

            SPListItemCollection documents = this.list.GetItems(query);
            foreach (SPListItem doc in documents){
                
                SPFile docFile = doc.File;

                AbstractFileLocalizationTask task = null;

                if (docFile != null && docFile.Length > 1)
                {
                    string Extension = Path.GetExtension(docFile.Name).ToLower();
                    if (Constants.FILE_WORD.Equals(Extension))
                    {
                            task = new WordFileLocalizationTask();
                    }
                    else if (Constants.FILE_POWERPOINT.Equals(Extension))
                    {
                            task = new PowerPointFileLocalizationTask();
                    }
                    else if (Constants.FILE_EXCEL.Equals(Extension))
                    {
                        task = new ExcelFileLocalizationTask();
                    }
                                        
                }

                
                if (task != null){                        
                        task.InitializeTask(FromLanguage, ToLanguage, doc);
                        
                    if (!String.IsNullOrEmpty(task.SourceText))
                            tasksToTranslate.Add(task);
                }

            }

            return tasksToTranslate;
        }

        internal IEnumerable<AbstractContent> Bind(SPList list)
        {
            if (list.BaseType != SPBaseType.DocumentLibrary)
                throw new ApplicationException(String.Format("List {0} is not a document library.", list.DefaultViewUrl));

            this.list = list;
            this.ContentTitle = String.IsNullOrEmpty(list.Title) ? list.ParentWebUrl : list.Title;
            this.ContentId = list.ID.ToString();
            this.ContentAddress = list.DefaultViewUrl;

            List<AbstractContent> BindResults = new List<AbstractContent>();
            BindResults.Add(this);

            return BindResults;
        }

        public override void ExportContentTranslations(BaseTranslationTask TranslationTask)
        {
            if (!(TranslationTask is AbstractFileLocalizationTask))
                throw new ArgumentException(String.Format("Illegal type {0} for translation task id {1}",
                   TranslationTask.GetType().ToString(), TranslationTask.TaskId));


            SPListItem ListItem = this.list.Items[((AbstractFileLocalizationTask)TranslationTask).ItemId];
            ((AbstractFileLocalizationTask)TranslationTask).ExportTranslation(ListItem);
        }

        public override void PerformExportDone()
        {
            
        }

        public void Dispose()
        {
            this.list = null;
        }
    }
}
